#include "simplefocparsepacket.h"
#include <QDebug>
#include "convert.h"

SimpleFOCParsePacket::SimpleFOCParsePacket(SimpleFOCProtocolData &data, QObject *parent)
    : BaseParsePacket (data, parent)
{

}

SimpleFOCParsePacket::~SimpleFOCParsePacket()
{

}

void SimpleFOCParsePacket::parse()
{
//    m_d->m_rxPacket.clear();
//    return;

//    QByteArray cmdSimple = m_d->m_rxPacket.simplified();
    QByteArray cmdSimple = m_d->m_rxPacket.buffer;
//    cmdSimple = cmdSimple.simplified();
//    cmdSimple.remove(QRegExp("\\s")); //去掉所有空格
    char cmd = cmdSimple.at(0);
    cmdSimple.remove(0, 1);
    QByteArrayList list;
    if (cmdSimple.contains('\t'))
        list = cmdSimple.split('\t');
//    int len = cmdSimple.size();
    switch (cmd)
    {
        case 'A':
//        if (len > 1)
        {
#if 0
            QByteArray cmd = cmdSimple.toUtf8();
            char *p = cmd.data();
            char dat[20];
            memcpy(dat, p + 1, len - 1);
            dat[len - 1] = '\0';
            float graphdata = Convert::String2Float(dat);
#else
//            qDebug() << list.at(0) << list.at(1);
            float graphdata = 0;
            float graphdata2 = 0;
            if (list.size() > 1)
            {
                graphdata = list.at(0).toFloat();
                graphdata2 = list.at(1).toFloat();
            }
            else
                graphdata = cmdSimple.toFloat();
#endif
            emit addGraphData(0, graphdata, graphdata2);
//            qDebug() << graphdata << graphdata2;
        }
        break;
        case 'T':
        break;
        default:
            emit receiveDebugData(m_d->m_rxPacket.buffer);
        break;
    }
    m_d->m_rxPacket.clear();
}
